package ch.electronet.wjw.web.actions.ajax.tree;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonBeanProcessor;
import ch.electronet.wjw.web.actions.JsonBaseAction;

public abstract class GetNodesAction extends JsonBaseAction {

	private static final long serialVersionUID = 1L;
	private String nodeId = null;

	public String getNodeId() {
		return nodeId;
	}

	public void setNodeId(String nodeId) {
		this.nodeId = nodeId;
	}

	@Override
	protected Object doJson(JsonConfig config) {
		config.registerJsonBeanProcessor(Category.class, nodeProcessor);
		return Category.getById(nodeId).getChildren();
	}

	protected JsonBeanProcessor nodeProcessor = new JsonBeanProcessor() {
		public JSONObject processBean(Object obj, JsonConfig config) {
			Node node = (Node) obj;
			JSONObject json = new JSONObject();
			json.element("id", node.getId()).element("label", node.getLabel()).element("isToggle", true).element("hasChildren", node.hasChildren());

			return json;
		}
	};
}
